package com.t114514.demo.mapper;

import com.t114514.demo.pojo.Answer;
import com.t114514.demo.pojo.StudentQuestionnaireTeacher;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface QuestionnaireAnswerMapper {

    @Insert("insert into Student_Questionnaire_Teacher (studentId,questionnaireId,teacherId,isValidate)" +
            " VALUES(#{studentId},#{questionnaireId},#{teacherId},#{isValidate}) ")
    void addStudentQuestionnaireTeacher(StudentQuestionnaireTeacher studentQuestionnaireTeacher);

    @Insert("INSERT INTO answer (answerContent,isValidate,teacherId,studentId,questionId) " +
            "VALUES (#{answerContent},#{isValidate},#{teacherId},#{studentId},#{questionId})")
    void addAnswer(Answer answer);

    @Select("SELECT * FROM answer where teacherId=#{teacherId} And studentId=#{studentId} And questionId=#{questionId}")
    Answer isExist(Answer answer);

    @Update("UPDATE answer set answerContent=#{answerContent} where teacherId=#{teacherId} And studentId=#{studentId} And questionId=#{questionId}")
    void updateAnswerContent(Answer answer);

    @Update("UPDATE answer set isValidate=1 " +
            "where teacherId=#{teacherId} And studentId=#{studentId} " +
            "And questionId IN (Select questionId from question where questionnaireId=#{questionnaireId})")
    void updateAllAnswer(StudentQuestionnaireTeacher studentQuestionnaireTeacher);

    @Update("update student_questionnaire_teacher set isValidate=1 " +
            "where teacherId=#{teacherId} And studentId=#{studentId} And questionnaireId=#{questionnaireId}")
    void updateStudentQuestionnaireTeacher(StudentQuestionnaireTeacher studentQuestionnaireTeacher);

    @Select("SELECT COUNT(*) From question where questionnaireId=#{questionnaireId} AND required= 1")
    int getQuestionNum(StudentQuestionnaireTeacher studentQuestionnaireTeacher);

    @Select("Select count(*) from answer  " +
            "where teacherId=#{teacherId} And studentId=#{studentId} " +
            "And questionId IN (Select questionId from question  where questionnaireId=#{questionnaireId} and required = 1)" )
    int getAnswerNum(StudentQuestionnaireTeacher studentQuestionnaireTeacher);

    @Select("Select * from answer where questionId=#{questionId} and teacherId=#{teacherId} and isValidate = 1")
    List<Answer> selectAnswerByQuestionId(Integer questionId,int teacherId);
}
